//
//  ZFK_squareMainViewController.m
//  Drops
//
//  Created by SHG on 16/5/20.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ZFK_squareMainViewController.h"
#import "AFNetworking.h"
#import "UIKit+AFNetworking.h"
#import "ZFK_header.h"
#import "ZFK_scrollPicView.h"
#import "ZFK_mainTableViewCell.h"
#import "UserViewController.h"
#import "ZFK_tagView.h"
#import "ZFK_tags.h"
#import "ZFK_choiseView.h"
#import "SelectionPost.h"
#import "ZFK_albumView.h"
#import "ZFK_album.h"
#import "ZFK_postView.h"
#import "ZFK_post.h"
#import "MJRefresh.h"
#import "ZFK_postCollectionViewCell.h"
#import "ZFK_postViewTableViewCell.h"
#import "MBProgressHUD.h"

@interface ZFK_squareMainViewController ()

<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, retain)NSMutableArray *scrollPicArray;    // 轮播图数组
@property (nonatomic, retain)NSMutableArray *tagArray;          // 标签数组
@property (nonatomic, retain)NSMutableArray *selectionArray;    // 精选数组
@property (nonatomic, retain)NSMutableArray *albumArray;        // 影集数组
@property (nonatomic, retain)NSMutableArray *postArray;         // 热门数组

@property(nonatomic, strong)UIButton *rightButton;
@property (nonatomic, assign)CGFloat height;
@property (nonatomic, assign)CGFloat viewHeight;

@property (nonatomic, retain)ZFK_scrollPicView *scrollPicView;
@property (nonatomic, retain)ZFK_tagView *tagView;
@property (nonatomic, retain)ZFK_choiseView *choiseView;
@property (nonatomic, retain)ZFK_albumView *albumView;
@property (nonatomic, retain)ZFK_postView *postView;

@property (nonatomic, assign)NSInteger number;
@property (nonatomic, retain)ZFK_post *post;
@property (nonatomic, assign)NSInteger hNumber;
@property (nonatomic, strong)UIImageView *image;

@property (nonatomic, retain)ZFK_album *b_album;
@property (nonatomic, retain)ZFK_album *sl_album;
@property (nonatomic, retain)ZFK_album *sm_album;
@property (nonatomic, retain)ZFK_album *sr_album;

@property (nonatomic, retain)MBProgressHUD *hud;

@end

@implementation ZFK_squareMainViewController

- (void)viewWillAppear:(BOOL)animated {
    [self.postView.mainCollectionView reloadData];
    [self.mainTableView reloadData];
    //LoginViewController  *vc = [[LoginViewController alloc]init];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.postArray = [NSMutableArray array];
    
    self.number = 85;
    self.hNumber = 0;
    
// 判断机型
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGFloat height = rect.size.height;
    
    if (height == 568.f) {
        self.height = 193;
    } else if (height == 667.f) {
        self.height = 230;
    } else if (height == 736.f) {
        self.height = 256;
    }
    
    [self createData];
//    [self createBannerData];
    [self createMainTableView];
    [self createScrollPicView];
    [self createTagView];
    [self createChoiseView];
    [self createAlbumView];
    [self createPostView];
    [self createBanner];
}
-(void)sendValue:(NSString *)str{
    self.str = str;
    [self.image setImageWithURL:[NSURL URLWithString:self.str]];
    NSLog(@"%@", self.str);
}

- (void)createData {
    [self getRequestWithString:@"http://www.fotoplace.cc/api/square/newIndex.do?lat=38.883491&lng=121.54447&city=0411&buildVersion=1&osNumber=1&clientVersion=3.2.1&seriesNumber=99000644068375&uid=25512642&token=e89ba406540046ccbfc816bb142cda852bc282b04bc844458cf9bf29899028b8" success:^(id jsonObject) {
        
//        [self createFlower];
        
        self.scrollPicArray = [NSMutableArray array];
        self.tagArray = [NSMutableArray array];
        self.selectionArray = [NSMutableArray array];
        self.albumArray = [NSMutableArray array];
        
        // 轮播图
        NSMutableArray *tempArr = jsonObject[@"data"][@"header"][@"activities"];
        [self.scrollPicArray addObject:tempArr.lastObject];
        for (NSInteger i = 0; i < tempArr.count; i++) {
            [self.scrollPicArray addObject:tempArr[i]];
        }
        [self.scrollPicArray addObject:tempArr.firstObject];
        self.scrollPicView.scrollPicArray = self.scrollPicArray;
        
        // 标签
        self.tagArray = jsonObject[@"data"][@"tags"][@"tagList"];
        self.tagView.tagsArr = self.tagArray;
        
        // 精选
        self.selectionArray = jsonObject[@"data"][@"selectionPost"][@"selectPostList"];
        self.choiseView.selectionArray = self.selectionArray;
        SelectionPost *post = [[SelectionPost alloc] init];
        [post setValuesForKeysWithDictionary:self.selectionArray[0]];
        [self.choiseView.imageView setImageWithURL:[NSURL URLWithString:post.postImg]];
        self.choiseView.textLabel.text = post.text;
        
        // 影集
        self.albumArray = jsonObject[@"data"][@"hotAlbum"][@"albumListItemDtos"];
        self.b_album = [[ZFK_album alloc] init];
        [self.b_album setValuesForKeysWithDictionary:self.albumArray[0]];
        self.sl_album = [[ZFK_album alloc] init];
        [self.sl_album setValuesForKeysWithDictionary:self.albumArray[1]];
        self.sm_album = [[ZFK_album alloc] init];
        [self.sm_album setValuesForKeysWithDictionary:self.albumArray[2]];
        self.sr_album = [[ZFK_album alloc] init];
        [self.sr_album setValuesForKeysWithDictionary:self.albumArray[3]];
        
        [self.albumView.bigImage setImageWithURL:[NSURL URLWithString:self.b_album.homePic]];
        self.albumView.bigTextLabel.text = self.b_album.title;
        [self.albumView.bigButton addTarget:self action:@selector(bigBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.albumView.s_leftImage setImageWithURL:[NSURL URLWithString:self.sl_album.homePic]];
        self.albumView.s_leftTextLabel.text = self.sl_album.title;
        [self.albumView.s_leftButton addTarget:self action:@selector(slBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.albumView.s_middleImage setImageWithURL:[NSURL URLWithString:self.sm_album.homePic]];
        self.albumView.s_middleTextLabel.text = self.sm_album.title;
        [self.albumView.s_middleButton addTarget:self action:@selector(smBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.albumView.s_rightImage setImageWithURL:[NSURL URLWithString:self.sr_album.homePic]];
        self.albumView.s_rightTextLabel.text = self.sr_album.title;
        [self.albumView.s_rightButton addTarget:self action:@selector(srBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
//        self.hud.hidden = YES;
        
        [self.mainTableView reloadData];
    }];
}

- (void)createBanner {
    self.number = 5885;
    [self refresh];
}

- (void)refresh {
    self.mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self createBannerData];
        [self createData];
        [self.mainTableView reloadData];
    }];
    [self.mainTableView.mj_header beginRefreshing];
    
    self.mainTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.number -= 10;
        self.hNumber ++;
        [self createBannerData];
        [self.mainTableView reloadData];
        [self.postView.mainCollectionView reloadData];
    }];
}

- (void)createBannerData {
    
    NSString *partUrl = @"30000&clientVersion=3.2.1&seriesNumber=99000644068375&version=3.2.1&uid=25512642&token=e89ba406540046ccbfc816bb142cda852bc282b04bc844458cf9bf29899028b8";
    
    [self getRequestWithString:[NSString stringWithFormat:@"http://www.fotoplace.cc/api/square/banner.do?categoryId=-1&fromDate=1463%ld%@", (long)self.number, partUrl] success:^(id jsonObject) {
        NSMutableArray *tempArr = jsonObject[@"data"][@"banners"];
//        for (NSDictionary *dic in tempArr) {
//            self.post = [[ZFK_post alloc] init];
//            [_post setValuesForKeysWithDictionary:dic[@"post"]];
//            [self.postArray addObject:_post];
//        }
        NSMutableArray *newArr = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in tempArr) {
            self.post = [[ZFK_post alloc] init];
            [self.post setValuesForKeysWithDictionary:dic[@"post"]];
            [newArr addObject:self.post];
        }
        [self.postArray addObjectsFromArray:newArr];
        
//        self.postView.postArray = self.postArray;
//        ZFK_postCollectionViewCell *cell = [[ZFK_postCollectionViewCell alloc] init];
//        [cell.imageView setImageWithURL:[NSURL URLWithString: self.post.postImg]];
        
        [self.mainTableView.mj_footer endRefreshing];
        [self.mainTableView.mj_header endRefreshing];
        [self.mainTableView reloadData];
//        [self.postView.mainCollectionView reloadData];
        
    }];
}

- (void)createMainTableView {
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    self.image.image = [UIImage imageNamed:@"user.png"];
    self.image.layer.cornerRadius = 22;
    self.image.layer.masksToBounds = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.image];
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(0, 0, 44, 44);
    self.rightButton.backgroundColor = [UIColor clearColor];
    //[self.rightButton setImage:[UIImage imageNamed:@"user.png"] forState:UIControlStateNormal];
    self.rightButton.layer.cornerRadius = 22;
    self.rightButton.layer.masksToBounds = YES;
    [self.image addSubview:self.rightButton];
    
    [self.rightButton addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT - 49 - 64) style:UITableViewStylePlain];
    self.mainTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainTableView];
    
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    [self.mainTableView registerClass:[ZFK_mainTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ZFK_mainTableViewCell class])];
    [self.mainTableView registerClass:[ZFK_postViewTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ZFK_postViewTableViewCell class])];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return sHEIGHT / 6;
            break;
        case 1:
            return sHEIGHT / 2 - sHEIGHT / 6;
            break;
        case 2:
            return sHEIGHT / 4 + sHEIGHT / 7 + 5;
            break;
        case 3:
            return (self.height) * (self.postArray.count / 2) - self.height * self.hNumber;
            break;
        default:
            return 100;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 3) {
        ZFK_postViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZFK_postViewTableViewCell class]) forIndexPath:indexPath];
        cell.postArray = [[NSMutableArray alloc] initWithArray:self.postArray];
        cell.mainCollectionView.frame = CGRectMake(0, 0, sWIDTH, (self.height) * (self.postArray.count / 2) - self.height * self.hNumber);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.mainCollectionView reloadData];
//        NSLog(@"cccccc:%ld", cell.postArray.count);
        return cell;
    } else if (indexPath.section == 0){
        ZFK_mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZFK_mainTableViewCell class]) forIndexPath:indexPath];
        cell.height = sHEIGHT / 6;
        [cell.baseView addSubview:self.tagView];
        return cell;
    } else if (indexPath.section == 1) {
        ZFK_mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZFK_mainTableViewCell class]) forIndexPath:indexPath];
        cell.height = sHEIGHT / 2 - sHEIGHT / 6;
        [cell.baseView addSubview:self.choiseView];
        return cell;
    } else {
        ZFK_mainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZFK_mainTableViewCell class]) forIndexPath:indexPath];
        cell.height = sHEIGHT / 4 + sHEIGHT / 7 + 5;
        [cell.baseView addSubview:self.albumView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"id====%@", )
}

// 瀑布流

- (void)createPostView {
     self.postView = [[ZFK_postView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, (self.height) * (self.postArray.count / 2) - self.height * self.hNumber)];
    self.postView.postArray = self.postArray;
    [self.postView.mainCollectionView reloadData];
    NSLog(@"height:%f", self.postView.frame.size.height);
}

// 轮播图
- (void)createScrollPicView {
    self.scrollPicView = [[ZFK_scrollPicView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT / 2 - sHEIGHT / 7)];
    
    self.scrollPicView.scrollPicArray = self.scrollPicArray;
    self.mainTableView.tableHeaderView = self.scrollPicView;
}

// 标签
- (void)createTagView {
    self.tagView = [[ZFK_tagView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT / 6)];
    self.tagView.tagsArr = self.tagArray;
}

// 精选
- (void)createChoiseView {
    self.choiseView = [[ZFK_choiseView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT / 2 - sHEIGHT / 6)];
    self.choiseView.selectionArray = self.selectionArray;
//    NSLog(@"counttttt:%ld", self.choiseView.selectionArray.count);
}

// 影集
- (void)createAlbumView {
    self.albumView = [[ZFK_albumView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT / 4 + sHEIGHT / 5)];
    
    [self.albumView.bigButton addTarget:self action:@selector(bigBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.albumView.s_leftButton addTarget:self action:@selector(slBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.albumView.s_middleButton addTarget:self action:@selector(smBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.albumView.s_rightButton addTarget:self action:@selector(srBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.albumView.bigImage.userInteractionEnabled = YES;
    self.albumView.s_leftImage.userInteractionEnabled = YES;
    self.albumView.s_middleImage.userInteractionEnabled = YES;
    self.albumView.s_rightImage.userInteractionEnabled = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, 20)];
    customView.backgroundColor = [UIColor whiteColor];
    
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
//    headerLabel.backgroundColor = [UIColor whiteColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor colorWithRed:88 / 255.0 green:91 / 255.0 blue:92 / 255.0 alpha:1];
    headerLabel.font = [UIFont systemFontOfSize:16];
    headerLabel.frame = CGRectMake(10, 0, sWIDTH - 20, 20);
    
    if (section == 0) {
        headerLabel.text = @"热门标签";
    } else if (section == 1) {
        headerLabel.text = @"热门精选";
    } else if (section == 2) {
        headerLabel.text = @"热门影集";
    } else if (section == 3) {
        headerLabel.text = @"热门足迹";
    }
    
    [customView addSubview:headerLabel];
    return customView;
}

-(void)rightButton:(UIButton *)button{
    UserViewController  *vc = [[UserViewController alloc]init];
    vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:vc animated:YES completion:^{
        //vc.delegate = self;

    }];
}

- (void)bigBtnClick:(UIButton *)button {
    NSLog(@"idddd===%@", self.b_album.albumId);
}

- (void)slBtnClick:(UIButton *)button {
    NSLog(@"idddd==%@", self.sl_album.albumId);
}

- (void)smBtnClick:(UIButton *)button {
    NSLog(@"idddd=%@", self.sm_album.albumId);
}

- (void)srBtnClick:(UIButton *)button {
    NSLog(@"iddd==%@", self.sr_album.albumId);
}

- (void)createFlower {
    self.hud = [[MBProgressHUD alloc] init];
    //    self.hud.labelText = @"你好";
    [self.view addSubview:self.hud];
    [self.hud show:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
